package com.lianniu.mgr.baseService.area.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.model.AdminAreaVo;
import com.lianniu.mgr.baseService.area.model.SystemAreaVo;
import com.lianniu.mgr.baseService.area.service.IAdminAreaService;
import com.lianniu.mgr.baseService.area.service.ISystemAreaService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.util.StringUtil;

/**
 * @ClassName：SysAreaRestController
 * @Description:系统区域Controller（只做数据传输）

 * @Since: 2016-03-30
 * @Version:1.0
 */
@Controller
@RequestMapping("sysAreaManager")
public class SysAreaRestController {
  private final static Logger logger = Logger.getLogger(SysAreaRestController.class);
  @Autowired
  private ISystemAreaService systemAreaService;
  @Autowired
  private IAdminAreaService adminAreaService;

  @RequestMapping(value={"/initSysAreaTree"},method={RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult initAdministrationAreaInfoTree(HttpServletRequest request){
    logger.info("getAllSysArea begin");
    @SuppressWarnings("unchecked")
    String areaId = request.getParameter("areaId");
    if(StringUtil.isBlank(areaId)){
      areaId = "0";
    }
    List<SystemAreaVo> list = (List<SystemAreaVo>)systemAreaService.getByParentId(areaId).getData();
    logger.info("getAllSysArea end");
    if(null!=list && list.size()>0){
      for(int i=0;i<list.size();i++){
        SystemAreaVo vo = list.get(i);
        if(vo.getAreaCode()==null){
          vo.setAreaCode("");
        }
        if(vo.getAreaDesc()==null){
          vo.setAreaDesc("");
        }
      }
    }
    return BaseResult.success(list);
  }
  @RequestMapping(value={"/initSysAddPage"},method={RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult initSysAddPage(HttpServletRequest request){
    @SuppressWarnings("unchecked")
    String id=request.getParameter("id");
    String level=request.getParameter("level");
    //初始化树
//    logger.info("getAllArea begin");
//    List<AdminAreaVo>list=  (List<AdminAreaVo>)adminAreaService.getAllArea().getData();
//    logger.info("getAllArea end");
    String parentId=request.getParameter("parentId");
    Map<String,Object> map=new HashMap<String,Object>();
//    map.put("tree", list);
    map.put("id", id);
    map.put("parentId", parentId);
    map.put("level", level);
    //初始化名字
//    Map<String,Object> map=new HashMap<String,Object>();
    if(level.equals("5")){
      SystemAreaVo vo=(SystemAreaVo)systemAreaService.getSysAreaByAreaId(id).getData();
      String zhenName=vo.getAreaName();
      SystemAreaVo vodi=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vo.getAreaId()).getData();
      String xianName=vodi.getAreaName();
      SystemAreaVo voci=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vodi.getAreaId()).getData();
      String shiName=voci.getAreaName();
      SystemAreaVo vopr=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(voci.getAreaId()).getData();
      String shengName=vopr.getAreaName();
      SystemAreaVo voco=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vopr.getAreaId()).getData();
      String gouName=voco.getAreaName();
      map.put("zhenName", zhenName);
      map.put("xianName", xianName);
      map.put("shiName", shiName);
      map.put("shengName", shengName);
      map.put("gouName", gouName);
      return BaseResult.success(map);
    }
    if(level.equals("4")){
      SystemAreaVo vo=(SystemAreaVo)systemAreaService.getSysAreaByAreaId(id).getData();
      String xianName=vo.getAreaName();
      SystemAreaVo voci=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vo.getAreaId()).getData();
      String shiName=voci.getAreaName();
      SystemAreaVo vopr=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(voci.getAreaId()).getData();
      String shengName=vopr.getAreaName();
      SystemAreaVo voco=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vopr.getAreaId()).getData();
      String gouName=voco.getAreaName();
      map.put("xianName", xianName);
      map.put("shiName", shiName);
      map.put("shengName", shengName);
      map.put("gouName", gouName);
      return BaseResult.success(map);
    }
    if(level.equals("3")){
      //先查出本市名字
      logger.info("getSysAreaByAreaId begin");
      SystemAreaVo vo=(SystemAreaVo) systemAreaService.getSysAreaByAreaId(id).getData();
      logger.info("getSysAreaByAreaId end");
      String shiName=vo.getAreaName();
      //再查出其父级，省的名字
      logger.info("getParentSysAreaByAreaId begin");
      SystemAreaVo vop=(SystemAreaVo) systemAreaService.getParentSysAreaByAreaId(id).getData();
      logger.info("getParentSysAreaByAreaId end");
      String shengName=vop.getAreaName();
      map.put("shengName", shengName);
      map.put("shiName", shiName);
    }
    if(level.equals("2")){
      //只查出本省的名字即可
      logger.info("getSysAreaByAreaId begin");
      SystemAreaVo vo=(SystemAreaVo) systemAreaService.getSysAreaByAreaId(id).getData();
      logger.info("getSysAreaByAreaId end");
      String shengName=vo.getAreaName();
      map.put("shengName", shengName);
    }
    if(level.equals("1")){
      return BaseResult.success(map);
    }
    return BaseResult.success(map);
  }

  @RequestMapping(value={"/initSysEditPage"},method={RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult initSysEditPage(HttpServletRequest request){
    String id=request.getParameter("id");
    String level=request.getParameter("level");
    String parentId=request.getParameter("parentId");
//    String adminAreaId=request.getParameter("adminAreaId");
//    logger.info("getSelectedArea begin");
//    @SuppressWarnings("unchecked")
//    //初始化树(有回显被选择项)
//    List<AdminAreaVo>list=  (List<AdminAreaVo>)adminAreaService.getSelectedArea(adminAreaId).getData();
//    logger.info("getSelectedArea end");
    Map<String,Object> map=new HashMap<String,Object>();
//    map.put("tree", list);
    map.put("id", id);
    map.put("parentId", parentId);
    map.put("level", level);
    //初始化名字
    if(level.equals("6")){
      SystemAreaVo vocu=(SystemAreaVo)systemAreaService.getSysAreaByAreaId(id).getData();
      String cunName=vocu.getAreaName();
      SystemAreaVo vozh=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vocu.getAreaId()).getData();
      String zhenName=vozh.getAreaName();
      SystemAreaVo vodi=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vozh.getAreaId()).getData();
      String xianName=vodi.getAreaName();
      SystemAreaVo voci=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vodi.getAreaId()).getData();
      String shiName=voci.getAreaName();
      SystemAreaVo vopr=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(voci.getAreaId()).getData();
      String shengName=vopr.getAreaName();
      SystemAreaVo voco=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vopr.getAreaId()).getData();
      String gouName=voco.getAreaName();
      map.put("cunName", cunName);
      map.put("zhenName", zhenName);
      map.put("xianName", xianName);
      map.put("shiName", shiName);
      map.put("shengName", shengName);
      map.put("gouName", gouName);
      return BaseResult.success(map);
    }
    if(level.equals("5")){
      SystemAreaVo vo=(SystemAreaVo)systemAreaService.getSysAreaByAreaId(id).getData();
      String zhenName=vo.getAreaName();
      SystemAreaVo vodi=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vo.getAreaId()).getData();
      String xianName=vodi.getAreaName();
      SystemAreaVo voci=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vodi.getAreaId()).getData();
      String shiName=voci.getAreaName();
      SystemAreaVo vopr=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(voci.getAreaId()).getData();
      String shengName=vopr.getAreaName();
      SystemAreaVo voco=(SystemAreaVo)systemAreaService.getParentSysAreaByAreaId(vopr.getAreaId()).getData();
      String gouName=voco.getAreaName();
      map.put("zhenName", zhenName);
      map.put("xianName", xianName);
      map.put("shiName", shiName);
      map.put("shengName", shengName);
      map.put("gouName", gouName);
      return BaseResult.success(map);
    }
    if(level.equals("4")){
      logger.info("getSysAreaByAreaId begin");
      SystemAreaVo voq=(SystemAreaVo) systemAreaService.getSysAreaByAreaId(id).getData();
      logger.info("getSysAreaByAreaId end");
      String xianName=voq.getAreaName();
      //先查出本市名字
      logger.info("getSysAreaByAreaId begin");
      SystemAreaVo vo=(SystemAreaVo) systemAreaService.getParentSysAreaByAreaId(id).getData();
      logger.info("getSysAreaByAreaId end");
      String shiName=vo.getAreaName();
      //再查出其父级，省的名字
      logger.info("getParentSysAreaByAreaId begin");
      SystemAreaVo vop=(SystemAreaVo) systemAreaService.getParentSysAreaByAreaId(vo.getAreaId()).getData();
      logger.info("getParentSysAreaByAreaId end");
      String shengName=vop.getAreaName();
      map.put("shengName", shengName);
      map.put("shiName", shiName);
      map.put("xianName", xianName);
    }
    if(level.equals("3")){
      //先查出本市名字
      logger.info("getSysAreaByAreaId begin");
      SystemAreaVo vo=(SystemAreaVo) systemAreaService.getSysAreaByAreaId(id).getData();
      logger.info("getSysAreaByAreaId end");
      String shiName=vo.getAreaName();
      //再查出其父级，省的名字
      logger.info("getParentSysAreaByAreaId begin");
      SystemAreaVo vop=(SystemAreaVo) systemAreaService.getParentSysAreaByAreaId(id).getData();
      logger.info("getParentSysAreaByAreaId end");
      String shengName=vop.getAreaName();
      map.put("shengName", shengName);
      map.put("shiName", shiName);
    }
    if(level.equals("2")){
      //只查出本省的名字即可
      logger.info("getSysAreaByAreaId begin");
      SystemAreaVo vo=(SystemAreaVo) systemAreaService.getSysAreaByAreaId(id).getData();
      logger.info("getSysAreaByAreaId end");
      String shengName=vo.getAreaName();
      map.put("shengName", shengName);
    }
    if(level.equals("1")){
      return BaseResult.success(map);
    }
    return BaseResult.success(map);
  }


  /**
   * @Description:校验区域编码是否重复
   * @param request
   * @return BaseResult
   * @Author:chenjun
   * @Date:2016年4月10日
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistSysAreaCode",method={RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult isExistAreaCode(HttpServletRequest request){
    String id=request.getParameter("id");
    String areaCode=request.getParameter("areaCode");
    logger.info("AreaRestController isExistAreaCode begin.");
    try{
      logger.info("isExistSysAreaCode begin.");
      BaseResult baseResult=systemAreaService.isExistSysAreaCode(id,areaCode);
      logger.info("isExistSysAreaCode end.");
      logger.info("AreaRestController isExistAreaCode end.");
      return baseResult;
    }catch(Exception e){
      logger.error("isExistSysAreaCode error:"+e.getMessage());
      logger.info("AreaRestController isExistSysAreaCode end.");
      return BaseResult.exception(e.getMessage());
    }
  }


  /**
   * @Description:用户数据权限（区域）
   * @param request
   * @param staffId
   * @return BaseResult
   * @Author:gukp
   * @Date:2016年9月29日上午10:49:27
   * @Version 1.0.0
   */
  @RequestMapping(value="/getPrecinctList",method=RequestMethod.POST)
  public @ResponseBody BaseResult getPrecinctList(HttpServletRequest request,String staffId){
    logger.info("SysAreaRestController getPrecinctList begin.");
    logger.info("员工id&staffId:"+staffId);

    //入参校验
    logger.info("SysAreaRestController getPrecinctList input data check begin.");
    if(StringUtil.isEmpty(staffId)){
      logger.error("入参staffId为null");
      logger.info("SysAreaRestController getPrecinctList input data check end.");
      logger.info("SysAreaRestController getPrecinctList end.");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("SysAreaRestController getPrecinctList input data check end.");

    try{
      logger.info("SysAreaRestController getPrecinctList getPrecinctTown begin.");
      BaseResult baseResult=systemAreaService.getPrecinctTown(staffId);
      logger.info("SysAreaRestController getPrecinctList getPrecinctTown end.");
      logger.info("SysAreaRestController getPrecinctList end.");
      return baseResult;
    }catch(Exception e){
      logger.error("SysAreaRestController getPrecinctList getPrecinctTown error:",e);
      logger.info("SysAreaRestController getPrecinctList end.");
      return BaseResult.exception(e.getMessage());
    }

  }


  /**
   * @Description:获取全国，省，市，县4级区域信息
   * @param isChecked
   * @return BaseResult
   * @Author:gukp
   * @Date:2016年10月18日上午10:12:14
   * @Version 1.0.0
   */
  @RequestMapping(value="/get4LevelAreas")
  public @ResponseBody BaseResult get4LevelAreas(String isChecked){
    logger.info("SysAreaRestController get4LevelAreas begin.");
    logger.info("isChecked&是否被选中："+isChecked);

    logger.info("SysAreaRestController get4LevelAreas input data check begin.");
    if(StringUtil.isEmpty(isChecked)){
      logger.info("入参isChecked为null");
      logger.info("SysAreaRestController get4LevelAreas input data check end.");
      logger.info("SysAreaRestController get4LevelAreas end.");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("SysAreaRestController get4LevelAreas input data check end.");

    try{
      logger.info("SysAreaRestController get4LevelAreas get4LevelAreas begin.");
      BaseResult baseResult=systemAreaService.get4LevelAreas(isChecked);
      logger.info("SysAreaRestController get4LevelAreas get4LevelAreas end.");
      return baseResult;
    }catch(Exception e){
      logger.error("SysAreaRestController get4LevelAreas get4LevelAreas error:",e);
      logger.info("SysAreaRestController get4LevelAreas end.");
      return BaseResult.exception(e.getMessage());
    }

  }

  @RequestMapping(value={"/addSystemTree"},method={RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult addSystemTree(HttpServletRequest request,SystemAreaVo vo){
    String parentId=request.getParameter("id");
    String parentLevel=request.getParameter("level");
    String username = request.getParameter("username");
    Long level= Long.parseLong(parentLevel);
    String areaId=StringUtil.getUUID();
    vo.setAreaId(areaId);
    vo.setParentId(parentId);
    vo.setAddUser(username);
    vo.setLevel(level+1);
    logger.info("addSysArea begin");
    int result=(int)systemAreaService.addSysArea(vo).getData();
    logger.info("addSysArea end");
    if(result<0){
      return BaseResult.fail(ErrorCode.SQLExceptionRetCode, ErrorCode.SQLExceptionRetInfo);
    }
    return BaseResult.success();
  }

  @RequestMapping(value={"/editSystemTree"},method={RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult EditSysTree(HttpServletRequest request,SystemAreaVo vo){
    String id=request.getParameter("id");
    String Stringlevel=request.getParameter("level");
    Long level= Long.parseLong(Stringlevel);
    String username = request.getParameter("username");
    vo.setAreaId(id);
    vo.setLevel(level);
    vo.setDelFlag("1");
    vo.setModifyUser(username);
    logger.info("editSysArea begin");
    int result=(int)systemAreaService.editSysArea(vo).getData();
    logger.info("editSysArea end");
    if(result<0){
      return BaseResult.fail(ErrorCode.SQLExceptionRetCode, ErrorCode.SQLExceptionRetInfo);
    }
    return BaseResult.success();
  }


}
